package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Models.InvPSOLines;
import Models.PSOLines;
import Models.PendingSalesOrder;

public class PendingSOPresenter extends AbstractPresenter<PendingSalesOrder> {
	final String SINGLEITEM = "pendingSO";
	final String MULTIITEM = "pendingSOes";
	
	public PendingSalesOrder getEmptyEntity(){
		PendingSalesOrder entity = new PendingSalesOrder();
		return entity;
	}
	
	public ArrayList<PendingSalesOrder> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public PendingSalesOrder tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public PendingSalesOrder createItemFromJSONObject(JSONObject jsonObject){
		PendingSalesOrder item = new PendingSalesOrder();
		try{
			item.setPsoID(jsonObject.getInt("psoid"));
			item.setPsoAllocationID(jsonObject.getInt("PSOAllocationID"));
			item.setPsoComplete(jsonObject.getBoolean("PSOComplete"));
			item.setPsoDate(timeFormat.parse(jsonObject.getString("PSODate")));
			item.setPsoXML(jsonObject.getString("psoxml"));
			item.setVersion(jsonObject.getInt("version"));
			
			CustomerPresenter custPresenter = new CustomerPresenter();
			JSONObject jsonCust = jsonObject.getJSONObject("PSOCustID");
			item.setPsoCustID(custPresenter.createItemFromJSONObject(jsonCust));
			
			//Set PSOLines Collection
			ArrayList<PSOLines> psoLines = new ArrayList<PSOLines>();
			if(!jsonObject.isNull("PSOLinesCollection")){
				Object itm = jsonObject.get("PSOLinesCollection");
				PSOLinePresenter presenter = new PSOLinePresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						psoLines.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					psoLines.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setPsoLines(psoLines);
			}
			
			//Set Documents Collection
			ArrayList<InvPSOLines> invPSOLines = new ArrayList<InvPSOLines>();
			if(!jsonObject.isNull("invPSOLinesCollection")){
				Object itm = jsonObject.get("invPSOLinesCollection");
				InvPSOLinePresenter presenter = new InvPSOLinePresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						invPSOLines.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					invPSOLines.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setInvPSOLines(invPSOLines);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(PendingSalesOrder item){
		try{
			JSONObject json = new JSONObject();
			json.put("psoid", item.getPsoID());
			json.put("PSOAllocationID", item.getPsoAllocationID());
			json.put("PSOComplete", item.isPsoComplete());
			json.put("PSODate", getDate(item.getPsoDate()));
			json.put("psoxml", item.getPsoXML());
			json.put("version", item.getVersion());
			
			CustomerPresenter custPresenter = new CustomerPresenter();
			json.put("PSOCustID", custPresenter.createJSON(item.getPsoCustID()));
			
			//Create PSOLines Collection
			if(item.getPsoLines()!=null){
				PSOLinePresenter presenter = new PSOLinePresenter();
				if(item.getPsoLines().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getPsoLines().size(); i++){
						jsonArray.put(presenter.createJSON(item.getPsoLines().get(i)));
					}
					json.put("PSOLinesCollection", jsonArray);
				}
			}
			
			//Create InvPSOLines Collection
			if(item.getInvPSOLines()!=null){
				InvPSOLinePresenter presenter = new InvPSOLinePresenter();
				if(item.getInvPSOLines().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getInvPSOLines().size(); i++){
						jsonArray.put(presenter.createJSON(item.getInvPSOLines().get(i)));
					}
					json.put("invPSOLinesCollection", jsonArray);
				}
			}
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
}
